package com.xumingxun.aibox.context;

import com.xumingxun.aibox.domain.var.Var;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

@Data
public class Context {
    List<Var> globalVars=new ArrayList<>();

    List<Var> runtimeVars=new ArrayList<>();

    public void setGlobalVarValue(Var var,Object value){
        Var var1=getVar(globalVars,var.getNodeName(),var.getExpression());
        if(var1==null){
            globalVars.add(var);
            var.setValue(value);
        }else{
            var1.setValue(value);
        }
    }
    public Object getGlobalVarValue(String nodeName,String expression){
        for (Var var : globalVars) {
            if (var.getNodeName().equals(nodeName)&&var.getExpression().equals(expression)) {
                return var.getValue();
            }
        }
        return null;
    }
    public Object getGlobalVarValue(Var var1){
        for (Var var : globalVars) {
            if (var.getNodeName().equals(var1.getNodeName())&&var.getExpression().equals(var1.getExpression())) {
                return var.getValue();
            }
        }
        return null;
    }

    public void setRuntimeVarValue(Var var,Object value){
        Var var1=getVar(runtimeVars,var.getNodeName(),var.getExpression());
        if(var1==null){
            runtimeVars.add(var);
            var.setValue(value);
        }else{
            var1.setValue(value);
        }
    }
    public Object getRuntimeVarValue(String nodeName,String expression){
        for (Var var : runtimeVars) {
            if (var.getNodeName().equals(nodeName)&&var.getExpression().equals(expression)) {
                return var.getValue();
            }
        }
        return null;
    }
    public Object getRuntimeVarValue(Var var1){
        for (Var var : runtimeVars) {
            if (var.getNodeName().equals(var1.getNodeName())&&var.getExpression().equals(var1.getExpression())) {
                return var.getValue();
            }
        }
        return null;
    }


    public Var getVar(List<Var> vars,String nodeName,String expression){
        for (Var var : vars) {
            if (var.getNodeName().equals(nodeName)&&var.getExpression().equals(expression)) {
                return var;
            }
        }
        return null;
    }
}
